package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

/**
 * Created by 80002023 on 2016/7/26.
 */
public class ScaleAndTransferDemo extends ApplicationAdapter {


    private Pixmap mPixmap;
    private Texture mTexture;
    private TextureRegion mTextureRegion;
    private SpriteBatch mSpriteBatch;
    private OrthographicCamera mCamera;

    private float mScaleX;
    private float mScaleY;
    private float mRotation;
    private float mTransfer;
    private float timer;

    @Override
    public void create() {

        mPixmap = new Pixmap(Gdx.files.internal("gremlins.png"));
        mTexture = new Texture(mPixmap);
        mTextureRegion = new TextureRegion(mTexture, 30, 0, 30, 30);
        mSpriteBatch = new SpriteBatch();
        // 镜头需要设定宽高和镜头中间位置
        mCamera = new OrthographicCamera(800, 480);
        mCamera.position.set(800 / 2, 480 / 2, 0);

        mScaleX = 1;
        mScaleY = 1;
        mRotation = 0;
        mTransfer = 0;
    }

    @Override
    public void resize(int width, int height) {
        // 确保AndroidManifest.xml里面的Activity配置为横屏
        // 宽和高才是横屏的值的返回，默认为竖屏的值
    }

    @Override
    public void render() {
        // 镜头的更新与设置矩阵到SpriteBatch
        mCamera.update();
        mSpriteBatch.setProjectionMatrix(mCamera.combined);

        timer += Gdx.graphics.getDeltaTime();
        // 1.移动
        if (timer < 3) {
            //每秒移动50像素
            mTransfer += 50 * Gdx.graphics.getDeltaTime();
        }
        // 2.放缩
        else if (timer > 3 && timer < 6) {
            mTransfer = 0;
            //每秒xy方向上放大1倍
            mScaleX += 1 * Gdx.graphics.getDeltaTime();
            mScaleY += 1 * Gdx.graphics.getDeltaTime();
        }
        // 3.翻转
        else if (timer > 6 && timer < 9) {
            mTransfer = 0;
            mScaleX = 1;
            mScaleY = 1;
            //每秒旋转90度
            mRotation += 90 * Gdx.graphics.getDeltaTime();
        } else {
            timer = 0;
            mTransfer = 0;
            mScaleX = 1;
            mScaleY = 1;
            mRotation = 0;
        }
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        mSpriteBatch.begin();
        //这里的15,15是放缩旋转的中心点， 他是根据纹理区域左下角来计算的
        //30,30是要要放缩到的大小，这里和剪切区域大小一致
        mSpriteBatch.draw(mTextureRegion, 400 + mTransfer, 240, 15, 15, 30, 30,
                mScaleX, mScaleY, mRotation);
        mSpriteBatch.end();
    }

    @Override
    public void dispose() {
        // 所有实现Disposable接口的都需要释放资源
        mPixmap.dispose();
        mTexture.dispose();
        mSpriteBatch.dispose();
    }
}
